Module.c("util");


util.getContextPath=function(){ 
	var pathName = document.location.pathname; 
	var index = pathName.substr(1).indexOf("/"); 
	var result = pathName.substr(0,index+1); 
	return result; 
}

util.fileSizeDisplay=function(size){
	var unit='KB';
	var sizeDisplay;
	
	if (size<1000000){
		sizeDisplay = size/1024;
		unit='KB';	
	}
	if (size>=1000000){
		sizeDisplay = (size/1024)/1000;
		unit='MB';
	}
	
	return Math.round(sizeDisplay*100)/100+unit;	
}




/**
 * ERROR HANDLER
 */
util.getErrorDesc=function(errorCode){
	
	if (errorCode=='errCodeUnknown') return '系统错误';
	if (errorCode=='errCodeNeedLogin') return '请先登录';
	if (errorCode=='errCodeNoSuchUser') return '没有该用户';
	if (errorCode=='errCodeWrongPassword') return '密码错误';
	
	return '未知错误(错误代码:'+errorCode+")";
}



util.popupErrorMsg=function(event,errorCode){
	var errorDesc =  util.getErrorDesc(errorCode);
	
	
	$.blockUI({ 
            message: '<h1>'+errorDesc+'</h1>'+'ERROR_CODE:'+errorCode, 
            fadeIn: 300, 
            fadeOut: 700, 
            timeout: 2000, 
            showOverlay: false, 
            centerY: false, 
            css: { 
                width: '180px', 
                top: '10px', 
                left: '', 
                right: '10px', 
                border: 'none', 
                padding: '5px', 
                backgroundColor: '#d33', 
                '-webkit-border-radius': '10px', 
                '-moz-border-radius': '10px', 
                opacity: .7, 
                color: '#fff' 
            } 
        }); 
	
}

/*
util.post=function(notifyEvent,url, data, callback, type){
	
	if ( jQuery.isFunction( data ) ) {
			type = type || callback;
			callback = data;
			data = {};
	}
	jQuery.ajax({
			type: "POST",
			url: url,
			data: data,
			success: function(data){postCallback(data,notifyEvent)},
			dataType: type
	});
}	

util.postCallback=function(data,notifyEvent){
	
	if (data.errCode=='errCodeNeedLogin'){
		alert('登录框');
		return;
	}
	
	if (!data.executeResult){
		popupErrorMsg("",data.errCode);
	}else{
		$("body").trigger(notifyEvent);
	}
}
*/


util.addSubmitToken = function(formEle,token){
	$(formEle).append("<input type='hidden' name='submitTokenIssuer' value='"+token.issuer+"'/>");
	$(formEle).append("<input type='hidden' name='submitTokenValue' value='"+token.value+"'/>");
}
util.updateSubmitToken=function(formEle,token){
	$('input[name=submitTokenIssuer]').val(token.issuer);
	$('input[name=submitTokenValue]').val(token.value);
}
util.resetForm=function(formEle){
	
	var formId=formEle.attr('id');
	
	var selector = '#'+formId+'  input[type=text]';
	selector += ','+'#'+formId+'  input[type=password]';
	selector += ','+'#'+formId+'  textarea';
	
	
	$(selector).each(function(){
		$(this).val('');
	})
}

util.popUploadDialog = function(option){
	
	plugin.uploadDialog.initComponent(option);
	$('#plugin_uploadDialog').dialog("open");
}

util.popLoginDialog = function(){
	$('#plugin_loginDialog').dialog("open");
}
